#include "widget.h"
#include "ui_widget.h"
#include <QTimer>
#include <thread>
Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    ui->lcdNumber->display(10);

//    time = new QTimer(this);

//    connect(time, &QTimer::timeout, this, &Widget::handle);
//    //开启定时器
//    time->start(1000);

//    int value = ui->lcdNumber->intValue();
//    while(true)
//    {
//        std::this_thread::sleep_for(std::chrono::seconds(1));
//        if(value <= 0)
//        {
//            break;
//        }
//       value -= 1;
//       ui->lcdNumber->display(value);
//    }

    std::thread t([this](){
        int value = ui->lcdNumber->intValue();
        while(true)
        {
            std::this_thread::sleep_for(std::chrono::seconds(1));
            if(value <= 0)
            {
                break;
            }
           value -= 1;
           ui->lcdNumber->display(value);
        }
    });
}

Widget::~Widget()
{
    delete ui;
}

void Widget::handle()
{
    int value = ui->lcdNumber->intValue();
    if(value <= 0)
    {
        time->stop();
        return;
    }
    ui->lcdNumber->display(value - 1);
}

